package message

import (
	"errors"
	dao2 "gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/dao"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/entity"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/global"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/model"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
	"strconv"
	"strings"
)

//********************************************
//Func  ：查询操作员的站内信列表
//Method：GET
//Usage ：
//********************************************
func QueryOperatorMessages(c *gin.Context) {

	pageSize, err1 := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
	if err1 != nil {
		global.Log.Error("QueryOperatorMessages input param pagesize error:", zap.Any("error", err1))
		c.JSON(http.StatusOK, model.Fail("请求参数错误"))
		return
	}

	page, err2 := strconv.Atoi(c.DefaultQuery("page", "1"))
	if err2 != nil {
		global.Log.Error("QueryOperatorMessages input param page error:", zap.Any("error", err2))
		c.JSON(http.StatusOK, model.Fail("请求参数错误"))
		return
	}

	receiverId, err := strconv.ParseInt(c.DefaultQuery("receiverId", ""), 10, 64)
	if err != nil {
		global.Log.Error("QueryOperatorMessages receiverId is lost.")
		c.JSON(http.StatusOK, model.Fail("未指名收件人"))
		return
	}

	readFlag, err := strconv.Atoi(c.DefaultQuery("readFlag", "0"))
	msgType, err := strconv.Atoi(c.DefaultQuery("msgType", "0"))

	total, msgs, err := dao2.QueryReceivedMsgList(page, pageSize, receiverId, readFlag, msgType)

	if err != nil {
		global.Log.Error("QueryAllMessages failed.", zap.Any("error", err))
		c.JSON(http.StatusOK, model.Fail("查询站内信失败"))
	} else {

		type ReceiveMsgList struct { //列表查询输出
			Total int64
			List  []*entity.ReceivingMessage
		}

		output := &ReceiveMsgList{
			Total: total,
			List:  msgs,
		}

		c.JSON(http.StatusOK, model.SuccessObject("操作成功", output))
		global.Log.Info("QueryAllMessages success.", zap.Any("label list", output))
	}
}

//********************************************
//Func  ：查询指定站内信详情
//Method：GET
//Usage ：
//********************************************
func QueryMessageDetail(c *gin.Context) {
	deptId, err := strconv.ParseInt(c.DefaultQuery("id", ""), 10, 64)
	if err != nil {
		global.Log.Error("QueryLabelDetail id is lost.")
		c.JSON(http.StatusOK, model.Fail("未指明查询站内信"))
		return
	}
	if deptId <= 0 {
		global.Log.Error("QueryLabelDetail deptid is lost.")
		c.JSON(http.StatusOK, model.Fail("未指明查询站内信"))
		return
	}

	label, err := dao2.QueryLabelDetail(deptId)
	if err != nil {
		global.Log.Error("QueryLabelDetail failed.", zap.Any("error", err))
		c.JSON(http.StatusOK, model.Fail("查询站内信详情失败"))
	} else {
		c.JSON(http.StatusOK, model.SuccessObject("操作成功", label))
		global.Log.Info("QueryLabelDetail success.", zap.Any("label detail", label))
	}
}

//********************************************
//Func  ：添加新站内信
//Method：Function
//Usage ：供内部业务接口调用
//********************************************
func CreateNewMessage(title, body, sender string, senderId int64, receiverIds []int64) error {
	var message entity.Message //发送邮件
	var err error

	if len(strings.TrimSpace(title)) <= 0 {
		return errors.New("title is none")
	}
	message.Title = title

	if len(body) <= 0 {
		return errors.New("body is none")
	}
	message.Body = body
	message.Sender = sender //发送人名称
	message.SenderId = senderId

	newmsg, err := dao2.CreateMessageToCustomer(title, body, sender, senderId, receiverIds)
	if err != nil {
		global.Log.Error("CreateMessageToCustomer failed.", zap.Any("error", err))
	} else {
		global.Log.Info("CreateMessageToCustomer success.", zap.Any("new message", newmsg))
	}
	return err
}
